/*
 * Block.java
 * 
 * last update: 15.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 *
 * Obs:
 */

package engine;

public class Block extends DefaultInstruction{


	public void execute()
	throws InterpretTimeException, SimulationStoppedException
	{
		if (state == 0) {
			waitToRun();
			xConsole.trace(this + " begin-> ");
		} else
			xConsole.trace(this + " <- end");
	}

	public Instruction next() {
		/*
		 * 0 means starting block execution
		 * 1 -//-- ending block execution
		 * 2 -//-- passing control to the next instruction
		 */
		xConsole.debug("cucu");
		switch (state) {
		case 0:
			state = 1;
			if (first == null)
				return this;
			else
				return first;
		case 1:
			state = 0;
			return next;
		}
		throw new Error("illegal instruction state");
	}

	DefaultInstruction first;

	public Block(NameSpace ns, int delayBefore, InstructionDescription desc,
			DefaultInstruction first, DefaultInstruction next){
		super(ns, delayBefore, desc);
		this.next = next;
		this.first = first;
		if (first != null)
			first.append(this);
	}

	public String toString(){
		//because desc is also a NameSpaceDescription, it must propagate the
		//lineNo, and thus the calling form  is a little bit tricky
		return ((BlockDescription)desc).toString(desc.line);
	}
}


